package com.relatev.minecraft.TRMobSpawn;

import com.relatev.minecraft.TRMobAPI.TRMob;
import com.relatev.minecraft.TRMobAPI.TRMobAPI;
import java.util.Iterator;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class CommandManager {

    public static boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0) {
            sender.sendMessage("§d作者缇亚祢|§eFor TRMobAPI");
            sender.sendMessage("§c/" + label + " help 查阅帮助");
            return true;
        }
        if (args[0].equalsIgnoreCase("help")) {
            sender.sendMessage("§espawnpoint 管理刷新点");
            sender.sendMessage("§ereload 重载插件");
        }
        if (sender.hasPermission("TRMobSpawn.admin")) {
            if (args[0].equalsIgnoreCase("reload")) {
                TRMobSpawn.MainPlugin.onDisable();
                TRMobSpawn.MainPlugin.onEnable();
                sender.sendMessage("§a重载已完成!");
            }
            if (args[0].equalsIgnoreCase("spawnpoint")) {
                if (args.length == 1) {
                    sender.sendMessage("§baddpoint <怪物ID> <间隔秒> (最低级) (最高级) 创建刷怪点");
                    sender.sendMessage("§bremovepoint <距离> 将距离多少M内所有刷怪点删除");
                    sender.sendMessage("§blistpoint (距离) 显示所有/半径内的刷怪点");
                    return true;
                }
                if (args[1].equalsIgnoreCase("addpoint")) {
                    TRMob trmob = TRMobAPI.getAPI().getTRMobManager().fuzzyRecognizeTRMob(args[2]);
                    if (trmob == null) {
                        sender.sendMessage("§c找不到怪物类型！");
                        return true;
                    }
                    int secondperiod = Integer.parseInt(args[3]);
                    int maxlevel = 1;
                    int minlevel = 1;
                    if (args.length > 4) {
                        maxlevel = Integer.parseInt(args[5]);
                        minlevel = Integer.parseInt(args[4]);
                    }
                    DataManager.SpawnPoints.put(((Player) sender).getLocation(), new TRSpawnPointInfo(new TRMobInfo(maxlevel, minlevel, trmob.ID), secondperiod));
                    sender.sendMessage("§a刷新点已被创建！");
                }
                if (args[1].equalsIgnoreCase("removepoint")) {
                    sender.sendMessage("§c开始删除刷新点.");
                    Player player = (Player) sender;
                    Iterator<Location> it = DataManager.SpawnPoints.keySet().iterator();
                    while (it.hasNext()) {
                        Location loc = it.next();
                        if (loc.getWorld() == player.getWorld() && player.getLocation().distance(loc) <= Integer.parseInt(args[2])) {
                            it.remove();
                            player.sendMessage("§b刷新点: §r" + showLocation(loc) + " §c已删除");
                        }
                    }
                }
                if (args[1].equalsIgnoreCase("listpoint")) {
                    sender.sendMessage("§6刷新点列表:");
                    Player player = (Player) sender;
                    if (args.length == 2) {
                        for (Location loc : DataManager.SpawnPoints.keySet()) {
                            player.sendMessage("§b刷新点: §a" + DataManager.SpawnPoints.get(loc).MobInfo.TRMobID + "§r-§9" + showLocation(loc));
                        }
                    } else {
                        for (Location loc : DataManager.SpawnPoints.keySet()) {
                            if (loc.getWorld() == player.getWorld() && player.getLocation().distance(loc) <= Integer.parseInt(args[2])) {
                                player.sendMessage("§b刷新点: §a" + DataManager.SpawnPoints.get(loc).MobInfo.TRMobID + "§r-§9" + showLocation(loc));
                            }
                        }
                    }
                }
            }
        }
        return true;
    }

    private static String showLocation(Location loc) {
        return loc.getWorld().getName() + ":" + loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ();
    }
}
